function addformmap()
{
   $("#addform").append('<div id="feature'+rowdiv+'" class="itPlg addMapPlg featureclass">'+dataform['map']+"</div>");
   var rownow=rowdiv;
   var rowchecknow=0;
   //initialize($('#feature'+rownow+' .mapdata'));
     var handler = function() {
     
    if(rowchecknow==0)
    {
    rowchecknow=1;
     $('#feature'+rownow+' .mapdata').gmap3({
          getgeoloc:{
            callback : function(latLng){
            
              if (latLng){
              
              $('#feature'+rownow+' input[name="lat[]"]').val(latLng.lat());
              $('#feature'+rownow+' input[name="lng[]"]').val(latLng.lng()); 
              
               // $('#test1-result').html('localised !');
                $(this).gmap3({
                  marker:{ 
                    latLng:latLng,
                    options:{
                        draggable: true
                    },
                    events:{
                        dragend: function(marker, event){
                        //$('input#lat').val( marker.position.lat() );
                        //$('input#lng').val( marker.position.lng() );
                        $('#feature'+rownow+' input[name="lat[]"]').val(marker.position.lat());
                        $('#feature'+rownow+' input[name="lng[]"]').val(marker.position.lng()); 
                        
                        
                        }
                        } 
                    
                    
                  },
                      map:{
                        options:{
                          zoom: 15,
                          center :latLng
                        }
                      }
                });
                
                
                $(this).unbind('click');
              } 
              else {

              }
            }
          }
        });
    }
        
      
     
     
     };
     
     $('#feature'+rownow+' .mapdata').bind('click', handler);
   
   
     $('#feature'+rownow+' input[name="detail[]"]').keyup(function() {
    
              if(rowchecknow==0)
              {
              rowchecknow=1;
              }
             // console.log(1);
              
             $('#feature'+rownow+' .mapdata').gmap3({
             
             
              getlatlng:{
                address:  $(this).val(),
                callback: function(results){
                
                  
                  
                  if ( !results ) return;
                  
                  
                  $(this).gmap3({
                    clear: {
                      name:["marker"],
                      last: true
                    }
                  });
                 
//console.log(results[0].geometry);
                 $('#feature'+rownow+' input[name="lat[]"]').val(results[0].geometry.location.lat());
                 $('#feature'+rownow+' input[name="lng[]"]').val(results[0].geometry.location.lng()); 
                  
                  $(this).gmap3({
                    marker:{
                      latLng:results[0].geometry.location,
                      options:{
                        draggable: true
                        },
                      events:{
                        dragend: function(marker, event){
                        //$('input#lat').val( marker.position.lat() );
                        //$('input#lng').val( marker.position.lng() );
                        $('#feature'+rownow+' input[name="lat[]"]').val(marker.position.lat());
                        $('#feature'+rownow+' input[name="lng[]"]').val(marker.position.lng()); 
                        
                        
                        }
                        }   
                        
                        
                    },
                      map:{
                        options:{
                          zoom: 15,
                          center :results[0].geometry.location
                        }
                      }
                  });
                  
                  
                  
                }
              }
              
              
              
              });  
              
     
     });
     
     
     $('#feature'+rownow+' .deleterowbutton').click(function(){
       
        $('#feature'+rownow).remove();
        
       });
  
  
   
  
   rowdiv++;
}
function saveformmap(id,plugid)
{
    var obj={};
    obj={plugid:plugid,lat:$("#"+id+' input[name="lat[]"]').val(),lng:$("#"+id+' input[name="lng[]"]').val(),detail:$("#"+id+' input[name="detail[]"]').val()};
    return obj;

}
function getformmap(dataplugin)
{
var code ='';
if(dataplugin.lat!='0.000000000000000')
{
code = '<div class="ldIt wrpMapLd clBg4 w640" id="mapid'+dataplugin.mapid+'">\n';
if(dataplugin.lat&&dataplugin.lng)
{

  code +='<a href="http://maps.google.com/?q='+dataplugin.lat+','+dataplugin.lng+'" target="_blank"><img width="100%"  src="http://maps.googleapis.com/maps/api/staticmap?center='+dataplugin.lat+','+dataplugin.lng+'&zoom=17&size=640x640&markers='+dataplugin.lat+','+dataplugin.lng+'&sensor=true" ></a>';

//code += '<iframe width="100%" height="340" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.co.th/maps?f=q&hl=en&geocode=&q='+dataplugin.lat+','+dataplugin.lng+'&aq=&sll='+dataplugin.lat+','+dataplugin.lng+'&ie=UTF8&ll='+dataplugin.lat+','+dataplugin.lng+'&spn=0.008774,0.026157&t=m&z=15&output=embed"></iframe><br /><small><a href="https://maps.google.co.th/maps?f=q&source=embed&hl=en&geocode=&q='+dataplugin.lat+','+dataplugin.lng+'&aq=&sll='+dataplugin.lat+','+dataplugin.lng+'&ie=UTF8&ll='+dataplugin.lat+','+dataplugin.lng+'&t=m&z=15" style="color:#0000FF;text-align:left">View Larger Map</a></small>';

//<iframe src="https://maps.google.co.th/maps?z=15&ll='+dataplugin.lat+','+dataplugin.lng+'" >';
}
else
{
code += '<div class="icoDefLd"  >\n';
code += '<span class="ico icoMapDefLd"></span>\n';
code += '<span>Map</span>\n';
code += '</div>\n';
}
code += '</div><input type="hidden" name="mapiddatanow[]" value="'+dataplugin.mapid+'"><input type="hidden" name="latdatanow[]" value="'+dataplugin.lat+'"><input type="hidden" name="lngdatanow[]" value="'+dataplugin.lng+'">';
}

return code;

}
function editformmap(datarow)  
{
             addformmap();
             var rownow= rowdiv-1;

              $('#feature'+rownow+' input[name="lat[]"]').val(datarow.lat);
              $('#feature'+rownow+' input[name="lng[]"]').val(datarow.lng); 
              $('#feature'+rownow+' input[name="detail[]"]').val(datarow.detail);
              
            //  $('#feature'+rownow+' .mapdata').gmap3();
              var latLng={lat:datarow.lat,lng:datarow.lng};
              $('#feature'+rownow+' .mapdata').gmap3({
                  marker:{ 
                    latLng:latLng,
                    options:{
                        draggable: true
                    },
                    events:{
                        dragend: function(marker, event){
                        //$('input#lat').val( marker.position.lat() );
                        //$('input#lng').val( marker.position.lng() );
                        $('#feature'+rownow+' input[name="lat[]"]').val(marker.position.lat());
                        $('#feature'+rownow+' input[name="lng[]"]').val(marker.position.lng()); 
                        
                        
                        }
                        } 
                    
                    
                  },
                      map:{
                        options:{
                          zoom: 15,
                          center :latLng
                        }
                      }
                });
              
              $('#feature'+rownow+' .mapdata').unbind('click');   

}

